import { createRouter, createWebHistory } from 'vue-router'
import NProgress from 'nprogress'
import { getAuth, setAuth } from '@/common/utils/storage'
import { useAuthStore } from '@/stores/modules/auth' // 添加这行导入
import { useUserStore } from '@/stores/modules/user' // 添加这行导入
import { useRootStore } from '@/stores/root' // 添加这行导入
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/', redirect: { name: 'email-login' }, meta: { name: '重定向' } },
    {
      path: '/email-login',
      name: 'email-login',
      component: () => import('../views/login/index.vue')
    },
    {
          path: '/account-login',
          name: 'account-password',
          component: () => import('@/views/login/components/other/zh/account-password.vue')
        },

    {
      path: '/layout',
      name: 'layout',
      component: () => import('../views/layout/index.vue'),
      meta: { requiresAuth: true }, // 添加这个meta标识
      children: [
        {
          path: '/conversation',
          name: 'conversation',
          component: () => import('../views/conversation/index.vue')
        },
        {
          path: '/friend',
          name: 'friend',
          component: () => import('../views/friend/index.vue')
        },
        {
          path: '/group',
          name: 'group',
          component: () => import('../views/group/index.vue')
        },
        {
          path: '/apply',
          name: 'apply',
          component: () => import('../views/apply/index.vue')
        },
      ],


beforeEnter: async (to, from, next) => {
      const authStore = useAuthStore()
      if (authStore.validateToken()) {
          await useUserStore().getUserInfo()
          next()
        } else {
          // 仅当从非登录页跳转时才清除数据
          if (from.path !== '/email-login' && from.path !== '/account-login') {
            const rootStore = useRootStore()
            rootStore.clearData()
          }
          next({ name: 'email-login', replace: true })
        }
      }
    }
  ]
})

// 修改全局前置守卫
router.beforeEach(async (to, from, next) => {
  const authStore = useAuthStore()
  const rootStore = useRootStore()
  if(!authStore.token||!authStore.user) {
    const authData = getAuth()
    if (authData.token && authData.userInfo) {
      authStore.$patch({
        token: authData.token,
        user: authData.userInfo
      })
    }
  }
  // 登录页特殊处理
  if(to.path === '/email-login' || to.path === '/account-login') {
    
    if (authStore.token && authStore.user) {
      next({ name: 'conversation' })
      return
    }
    next()
    return
  }
  
  // 允许匿名访问的路由
  if(to.matched.some(record=>record.meta.allowAnonymous)) {
    next()
    return
  }
  
  // 检查token
  if (!authStore.token) {
    // 仅当从非登录页跳转时才清除数据
    if (from.path !== '/email-login' && from.path !== '/account-login') {
      rootStore.clearData()
    }
    next({ path: '/email-login' })
    return
  }
  
  // 检查token有效性
  if (!authStore.validateToken()) {
    rootStore.clearData()
    next({ path: '/email-login' })
    return
  }
  
  next()
})
router.afterEach(() => {
  NProgress.done()
})

export default router
